﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GestorDocument.Model;
using GestorDocument.ViewModel.AsuntoTurno;

namespace GestorDocument.UI.AsuntoTurno
{
    /// <summary>
    /// Lógica de interacción para ConsultaSeguimientoTurnoView.xaml
    /// </summary>
    public partial class ConsultaSeguimientoTurnoView : UserControl
    {
        TrancingAsuntoViewModel _TrancingAsuntoViewModel;
        public ConsultaSeguimientoTurnoView()
        {
            InitializeComponent();
        }

        private TrancingAsuntoViewModel ConvertDataContext(object dataSource)
        {
            TrancingAsuntoViewModel viewModel = dataSource as TrancingAsuntoViewModel;
            return viewModel;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var li in this.ListSeguimientoTurno.Items)
            {
                TurnoModel turno = li as TurnoModel;
                if (turno.IsAtendido || turno.IsTurnado)
                {
                    turno.IsEnabled = true;
                }

                turno.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(turno_PropertyChanged);
            }
        }

        void turno_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsClick" && (sender as TurnoModel).IsClick)
            {
                
                _TrancingAsuntoViewModel = ConvertDataContext(this.DataContext);

                if (_TrancingAsuntoViewModel != null)
                {
                    RechazarTurnoView rechazarTurno = new RechazarTurnoView();
                    _TrancingAsuntoViewModel.SelectedSeguimientoTurnos = (sender as TurnoModel);
                    
                    _TrancingAsuntoViewModel.RechazarTurno = _TrancingAsuntoViewModel.SelectedSeguimientoTurnos;
                    _TrancingAsuntoViewModel.RechazarTurno.RechazoComentario = (!string.IsNullOrWhiteSpace(_TrancingAsuntoViewModel.SelectedSeguimientoTurnos.RechazoComentario)) ? _TrancingAsuntoViewModel.SelectedSeguimientoTurnos.RechazoComentario + "\r" : _TrancingAsuntoViewModel.SelectedSeguimientoTurnos.RechazoComentario;
                    rechazarTurno.DataContext = _TrancingAsuntoViewModel;
                    rechazarTurno.ShowDialog();
                    
                }

                (sender as TurnoModel).IsClick = false;
            }
        }
    }
}
